package com.xiyu.service.service.material;

import com.xiyu.service.vo.material.warehouse.WarehouseSimpleListOutput;
import com.xiyu.service.vo.material.warehouse.WarehouseSingleGetOutput;
import com.xiyu.service.vo.material.warehouse.WarehouseUpdateInput;
import com.xiyu.service.vo.material.warehouse.WarehouseCreateInput;
import com.xiyu.service.model.material.info.MaterialWarehouse;
import com.xiyu.service.repository.material.info.MaterialWarehouseRepository;
import com.xiyu.service.vo.material.warehouse.WarehousePageQueryOutput;
import com.xiyu.service.vo.material.warehouse.WarehousePageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.material.WarehouseConvert;
import com.xiyu.service.util.entity.EntityUtils;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.material.WarehouseErrorCode.*;

/**
 * 供应商仓库 Service 实现类
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private MaterialWarehouseRepository materialWarehouseRepository;



    @Override
    public PageResult<WarehousePageQueryOutput> pageQuery(WarehousePageQueryInput inputVO) {
        Page<MaterialWarehouse> pageMaterialWarehouse = materialWarehouseRepository.pageQuery(inputVO);
        List<WarehousePageQueryOutput> listMaterialWarehouse = WarehouseConvert.INSTANCE.pageQueryOutputConvert(pageMaterialWarehouse.getRows());
        return new PageResult<>(listMaterialWarehouse, pageMaterialWarehouse.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(WarehouseCreateInput inputVO) {
        Optional<MaterialWarehouse> optionalDuplicateMaterialWarehouse = materialWarehouseRepository.findFirstByWarehouseName(inputVO.getWarehouseName());
        if(optionalDuplicateMaterialWarehouse.isPresent()){
            throw exception(MATERIAL_WAREHOUSE_NAME_DUPLICATE);
        }
        MaterialWarehouse newMaterialWarehouse = WarehouseConvert.INSTANCE.createInputConvert(inputVO);
        newMaterialWarehouse = materialWarehouseRepository.insert(newMaterialWarehouse);
        return newMaterialWarehouse.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(WarehouseUpdateInput inputVO) {
        Optional<MaterialWarehouse> optionalDuplicateMaterialWarehouse = materialWarehouseRepository.findFirstByWarehouseName(inputVO.getWarehouseName());
        if(optionalDuplicateMaterialWarehouse.isPresent() && !inputVO.getId().equals(optionalDuplicateMaterialWarehouse.get().id())){
            throw exception(MATERIAL_WAREHOUSE_NAME_DUPLICATE);
        }
        Optional<MaterialWarehouse> optionalOldMaterialWarehouse = materialWarehouseRepository.findById(inputVO.getId());
        if(!optionalOldMaterialWarehouse.isPresent()){
            throw exception(MATERIAL_WAREHOUSE_NOT_EXIST);
        }
        MaterialWarehouse updateMaterialWarehouse = WarehouseConvert.INSTANCE.updateInputConvert(inputVO);
        materialWarehouseRepository.update(updateMaterialWarehouse);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleted(Long id) {
        Optional<MaterialWarehouse> optionalDeleteMaterialWarehouse = materialWarehouseRepository.findById(id);
        if(!optionalDeleteMaterialWarehouse.isPresent()){
            throw exception(MATERIAL_WAREHOUSE_NOT_EXIST);
        }
        materialWarehouseRepository.deleteById(id);
        return true;
    }

    @Override
    public WarehouseSingleGetOutput singleGet(Long id) {
        Optional<MaterialWarehouse> optionalMaterialWarehouse = materialWarehouseRepository.findById(id);
        if(!optionalMaterialWarehouse.isPresent()){
            throw exception(MATERIAL_WAREHOUSE_NOT_EXIST);
        }
        return WarehouseConvert.INSTANCE.singleGetOutputConvert(optionalMaterialWarehouse.get());
    }

    @Override
    public List<WarehouseSimpleListOutput> simpleList() {
        List<MaterialWarehouse> materialWarehouseList = materialWarehouseRepository.findAll();
        return WarehouseConvert.INSTANCE.simpleListOutputConvert(materialWarehouseList);
    }

}
